package com.leoren.run.activity.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.leoren.run.R;
import com.leoren.run.activity.share.CommentAdapter;
import com.leoren.run.common.ResponseCode;
import com.leoren.run.common.ServerResponse;
import com.leoren.run.common.UrlConstants;
import com.leoren.run.log.L;
import com.leoren.run.utils.GsonUtil;
import com.leoren.run.utils.Http.OkHttp.CommonOkHttpClient;
import com.leoren.run.utils.Http.OkHttp.exception.OkHttpException;
import com.leoren.run.utils.Http.OkHttp.listener.DisposeDataHandle;
import com.leoren.run.utils.Http.OkHttp.listener.DisposeDataListener;
import com.leoren.run.utils.Http.OkHttp.request.CommonRequest;
import com.leoren.run.utils.ToastUtil;
import com.leoren.run.vo.CommentVo;

import java.util.List;

import okhttp3.Request;

/**
 * 社区分享界面
 */
public class ShareFragment extends Fragment {

    private RecyclerView commentListView;
    private List<CommentVo> commentVos;
    private LinearLayoutManager manager;
    private CommentAdapter adapter;

    private View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_share, container, false);

        initData();

        initViews();

        return rootView;
    }

    /**
     * 初始化控件
     */
    private void initViews(){
        commentListView = rootView.findViewById(R.id.comment_list_view);

    }

    /**
     * 网络请求社区数据
     */
    private void initData(){
        Request request = CommonRequest.createGetRequest(UrlConstants.GET_ALL_COMMENT);
        CommonOkHttpClient.post(request, new DisposeDataHandle(new DisposeDataListener() {
            @Override
            public void onSuccess(String responseStr) {
                ServerResponse<List<CommentVo>> response = GsonUtil.formatJsonToCommentListVo(responseStr);
                L.e("commentVoList = " + response.toString());
                if(response == null){
                    onFailure(new OkHttpException(ResponseCode.ERROR.getCode(), ResponseCode.NETWORK_ERROR.getValue()));
                    return;
                }
                if(response.isSuccess()){
                    commentVos = response.getData();
                    L.e("pathData = " + commentVos);
                    initListView();
                }else {
                    ToastUtil.MakeToast(response.getMsg());
                }
            }

            @Override
            public void onFailure(OkHttpException e) {
                ToastUtil.MakeToast(e.getMsg().toString());
            }
        }));
    }

    /**
     * 展示分享数据
     */
    private void initListView(){
        adapter = new CommentAdapter(commentVos, this.getContext());
        manager = new LinearLayoutManager(this.getContext());
        commentListView.setLayoutManager(manager);
        commentListView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

}
